<?php

namespace App\Models;

use App\Models\GoodsCustomRent;
use App\Services\CommonService;
use App\Services\GoodsService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class GoodsSpec extends Model
{
    use HasFactory;

    protected $table = 'sp_goods_spec_2';
    protected $primaryKey = 'id';
    public $timestamps = false;

    protected function GetGoodsSpecDetailsDataByGoodid($goods_id)
    {
        $select = array('gsp.id as specs_id', 'gsp.lease_price', 'gsp.lease_time',
            'gs.id', 'gs.title', 'gs.lease_set', 'gs.low_lease', 'gs.quantity', 'gs.foregift', 'gs.is_buy_out', 'gs.price');
        $data = $this
            ->from('sp_goods_spec_2 as gsp')
            ->select($select)
            ->leftjoin('sp_goods_setmeal_2 as gs', 'gs.id', '=', 'gsp.setmeal_id')
            ->where([['gsp.goods_id', $goods_id],['gsp.is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])
            ->get();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //获取商品最低租金
    protected function GetMinPrice($goods_id)
    {
        $min_price = $this->where([['goods_id', $goods_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])
            ->min('day_price');
        return $min_price;
    }

    protected function GetSetmealMinLeaseTerm($goods_id,$setmeal_id,$lease_set)
    {
        $min_price = $this->where([['goods_id', $goods_id],['setmeal_id', $setmeal_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])
            ->min('lease_time');
        if($lease_set == GoodsService::GOODS_RENT_TYPE_MONTH){ //按月
            $min_price = $min_price * 30;
        }
        return $min_price;
    }

    //小程序检测租期获取租期信息
    protected function CheckGoodsSpec($goods_id,$setmeal_id,$spec_id){
        $spec = $this->where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['id',$spec_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])
            ->select('lease_time','lease_price','day_price')->first();
        $spec = $spec ? $spec->toArray() : [];
        return $spec;
    }

    //小程序商品详情获取租期信息
    protected function GetGoodsSpecData($goods_id,$setmeal_id,$lease_set,$is_huabei){
        $spec = $this->where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])
            ->select('id','lease_time','lease_price','withhold_cycle','day_price','encoding')->get();
        $spec = $spec ? $spec->toArray() : array();
        if(!empty($spec)){
            foreach ($spec as $k=>$v){
                if($lease_set == GoodsService::GOODS_RENT_TYPE_CUSTOM){ //自定义租期
                    $custom_rent = GoodsCustomRent::where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['spec_id',$v['id']],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->select('which_period','lease_days','lease_price','day_price')->get();
                    $custom_rent = $custom_rent ? $custom_rent->toArray() : array();
                    $spec[$k]['lease_title'] = $v['lease_time'].'天('.count($custom_rent).'期，每期'.$custom_rent[0]['lease_days'].'天)';
                    $spec[$k]['first_rent'] = $custom_rent[0]['day_price'];
                    $spec[$k]['total_amount'] = sprintf("%01.2f",array_sum(array_column($custom_rent,'day_price')));
                }elseif ($lease_set == GoodsService::GOODS_RENT_TYPE_MONTH){//按月
                    $spec[$k]['lease_title'] = $v['lease_time'].'月';
                    $spec[$k]['first_rent'] = $v['day_price'];
                    $spec[$k]['total_amount'] = sprintf("%01.2f",$v['day_price'] * $v['lease_time']);
                }elseif ($lease_set == GoodsService::GOODS_RENT_TYPE_DAY){//按天
                    $spec[$k]['lease_title'] = $v['lease_time'].'天';
                    $spec[$k]['total_amount'] = sprintf("%01.2f",$v['day_price'] * $v['lease_time']);
                    if($v['lease_time'] <= 30){
                        $spec[$k]['first_rent'] = sprintf("%01.2f", $v['lease_price']);
                    }else{
                        $spec[$k]['first_rent'] = $v['lease_price']>0?sprintf("%01.2f", $v['lease_price']):sprintf("%01.2f",$v['day_price'] * 30);
                    }
                }
                if($is_huabei == GoodsService::GOODS_SETMEAL_IS_HUABEI){
                    $huabei_data = GoodsHuabei::GetHuabeiData($goods_id,$setmeal_id,$v['id']);
                    if(!empty($huabei_data)){
                        if(!empty($huabei_data['three_bear']) && $huabei_data['three_bear'] == GoodsService::GOODS_HUABEI_FEE_USER && !empty($huabei_data['three_fee'])){ //用户承担服务费
                            $huabei_data['three_stages_amount'] = round(($huabei_data['three_fee'] + $huabei_data['rent_total']) / 3,4);
                        }elseif (!empty($huabei_data['three_bear']) && $huabei_data['three_bear'] == GoodsService::GOODS_HUABEI_FEE_MERCHANT && !empty($huabei_data['three_fee'])){//商户承担手续费
                            $huabei_data['three_stages_amount'] = round($huabei_data['rent_total'] / 3,4);
                        }
                        if(!empty($huabei_data['six_bear']) && $huabei_data['six_bear'] == GoodsService::GOODS_HUABEI_FEE_USER && !empty($huabei_data['six_fee'])){ //用户承担服务费
                            $huabei_data['six_stages_amount'] = round(($huabei_data['six_fee'] + $huabei_data['rent_total']) / 6,4);
                        }elseif(!empty($huabei_data['six_bear']) && $huabei_data['six_bear'] == GoodsService::GOODS_HUABEI_FEE_MERCHANT && !empty($huabei_data['six_fee'])){ //商户承担手续费
                            $huabei_data['six_stages_amount'] = round($huabei_data['rent_total']/ 6,4);
                        }
                        if(!empty($huabei_data['twelve_bear']) && $huabei_data['twelve_bear'] == GoodsService::GOODS_HUABEI_FEE_USER && !empty($huabei_data['twelve_fee'])){ //用户承担服务费
                            $huabei_data['twelve_stages_amount'] = round(($huabei_data['twelve_fee'] + $huabei_data['rent_total']) / 12,4);
                        }elseif(!empty($huabei_data['twelve_bear']) && $huabei_data['twelve_bear'] == GoodsService::GOODS_HUABEI_FEE_MERCHANT && !empty($huabei_data['twelve_fee'])){ //商户承担手续费
                            $huabei_data['twelve_stages_amount'] = round($huabei_data['rent_total'] / 12,4);
                        }
                    }
                    $spec[$k]['huabei_data'] = $huabei_data;
                }
            }
        }

        return $spec;
    }

    //获取租期列表
    protected function GetSpecList($setmeal_id,$goods_id){
        $data = $this->where([['setmeal_id',$setmeal_id],['goods_id',$goods_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->select('id','lease_time','lease_price','day_price','encoding')->get();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //添加商品租期信息
    protected function AddGoodsSpec($goods_spec,$goods_id,$setmeal_id,$lease_set,$goods_lease_time=''){
        foreach ($goods_spec as $k=>$v){
            if($lease_set == GoodsService::GOODS_RENT_TYPE_CUSTOM){ //自定义租金
                if(!empty($v)){
                    $lease_time = array_sum(array_column($v,'lease_days'));//租期
                    $lease_price = array_sum(array_column($v,'lease_price'));//租期总租金
                    $withhold_cycle = $v[0]['withhold_cycle'];//扣款周期
                    $spec = $this->where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['lease_time',$lease_time],['withhold_cycle',$withhold_cycle],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->select('id')->first();
                    $spec = $spec ? $spec->toArray() : array();
                    if(empty($spec)){
                        //租期
                        $lease_time_num = (int)$lease_time;
                        $lease_num = GoodsService::GOODS_LEASE_DAY_LIST[$lease_time_num];
                        //每期价格 总租金/租期
                        $lease_price1 = sprintf("%01.2f",$lease_price/$lease_num);

                        $spec_data = [];
                        $spec_data['goods_id'] = $goods_id;
                        $spec_data['setmeal_id'] = $setmeal_id;
                        $spec_data['lease_time'] = $goods_lease_time? $goods_lease_time :$lease_time;
                        $spec_data['lease_price'] = $lease_price1;
                        $spec_data['encoding'] = $v['encoding'];
                        $spec_data['day_price'] = bcdiv($lease_price,$spec_data['lease_time'],2);
                        $spec_data['withhold_cycle'] = $withhold_cycle;
                        $spec_data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                        $spec_id = $this->insertGetId($spec_data);
                        //自定义租金
                        if($spec_id){
                            GoodsCustomRent::AddCustomRent($goods_id,$setmeal_id,$spec_id,$v);
                        }
                    }
                }
            }else{
                $v['lease_time']=$goods_lease_time? $goods_lease_time :$v['lease_time'];
                if(!empty($v['lease_time'])){
                    $spec = $this->where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['lease_time',$v['lease_time']],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->select('id')->first();
                    $spec = $spec ? $spec->toArray() : array();
                    if(empty($spec)||!empty($goods_lease_time)){
                        // dd($v);
                        $lease_time = $v['lease_time'];//期数 365
                        $lease_price = $v['day_price'];//日租金
                        //租期
                        $lease_time_num = (int)$lease_time;
                        $lease_num = GoodsService::GOODS_LEASE_DAY_LIST[$lease_time_num];
                        // echo $lease_num;
                        //总租金
                        $all_price = $lease_time_num*$lease_price;

                        //每期价格 总租金/租期
                        $lease_price1 = sprintf("%01.2f",$all_price/$lease_num);
                        // dd($lease_price1);
                        $spec_data = [];
                        $spec_data['goods_id'] = $goods_id;
                        $spec_data['setmeal_id'] = $setmeal_id;
                        $spec_data['encoding'] = $v['encoding'];
                        $spec_data['day_price'] = $v['day_price'];
                        $spec_data['lease_time'] = $v['lease_time'];
                        $spec_data['lease_price'] = $lease_price1;
                        $spec_data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                        if($lease_set == GoodsService::GOODS_RENT_TYPE_MONTH){
                            $spec_data['day_price'] = bcdiv($v['day_price'],30,2);
                        }else{
                            $spec_data['day_price'] = $v['day_price'];
                        }
                        // dd($spec_data);
                        $spec_id = $this->insertGetId($spec_data);
                        // dd($spec_id);

                    }
                }
            }

        }
    }

    //编辑商品租期信息
    protected function EditGoodsSpec($goods_spec,$goods_id,$setmeal_id,$lease_set,$goods_lease_time=''){
        $this->where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['is_delete',CommonService::GOODS_IS_DELETE_NOT]])->update(['is_delete'=>CommonService::GOODS_IS_DELETE_YES]);
        GoodsHuabei::where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['is_delete',CommonService::GOODS_IS_DELETE_NOT]])->update(['is_delete'=>CommonService::GOODS_IS_DELETE_YES]);
        if($lease_set == GoodsService::GOODS_RENT_TYPE_CUSTOM){
            GoodsCustomRent::where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['is_delete',CommonService::GOODS_IS_DELETE_NOT]])->update(['is_delete'=>CommonService::GOODS_IS_DELETE_YES]);
        }
        foreach ($goods_spec as $k=>$v){
            if($lease_set == GoodsService::GOODS_RENT_TYPE_CUSTOM){ //自定义租金
                if(!empty($v)){
                    $lease_time = array_sum(array_column($v,'lease_days'));//租期
                    $lease_price = array_sum(array_column($v,'lease_price'));//租期总租金
                    $withhold_cycle = $v[0]['withhold_cycle'];//扣款周期
                    if(isset($v[0]['id'])){
                        $spec_id = GoodsCustomRent::where('id',$v[0]['id'])->value('spec_id');
                        if(!empty($spec_id)){
                            $spec_data = [];
                            $spec_data['lease_time'] = $lease_time;
                            $spec_data['lease_price'] = round($lease_price/$lease_time,2);
                            $spec_data['day_price'] = bcdiv($lease_price,$lease_time,2);
                            $spec_data['withhold_cycle'] = $withhold_cycle;
                            $spec_data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                            $this->where('id',$spec_id)->update($spec_data);
                            //自定义租金
                            GoodsCustomRent::EditCustomRent($goods_id,$setmeal_id,$spec_id,$v);
                        }else{
                            $spec_data = [];
                            $spec_data['goods_id'] = $goods_id;
                            $spec_data['setmeal_id'] = $setmeal_id;
                            $spec_data['lease_time'] = $lease_time;
                            $spec_data['lease_price'] = round($lease_price/$lease_time,2);
                            $spec_data['day_price'] = bcdiv($lease_price,$lease_time,2);
                            $spec_data['withhold_cycle'] = $withhold_cycle;
                            $spec_data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                            $spec_id = $this->insertGetId($spec_data);
                            //自定义租金
                            if($spec_id){
                                GoodsCustomRent::AddCustomRent($goods_id,$setmeal_id,$spec_id,$v);
                            }
                        }
                    }else{
                        $spec_data = [];
                        $spec_data['goods_id'] = $goods_id;
                        $spec_data['setmeal_id'] = $setmeal_id;
                        $spec_data['lease_time'] = $lease_time;
                        $spec_data['lease_price'] = round($lease_price/$lease_time,2);
                        $spec_data['day_price'] = bcdiv($lease_price,$lease_time,2);
                        $spec_data['withhold_cycle'] = $withhold_cycle;
                        $spec_data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                        $spec_id = $this->insertGetId($spec_data);
                        //自定义租金
                        if($spec_id){
                            GoodsCustomRent::AddCustomRent($goods_id,$setmeal_id,$spec_id,$v);
                        }
                    }
                }
            }else{
                $v['lease_time']=$goods_lease_time?$goods_lease_time:$v['lease_time'];
                if(!empty($v['lease_time'])){
                    $spec_id = $this->where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['lease_time',$v['lease_time']]])->value('id');

                    if(!empty($spec_id)&&empty($goods_lease_time)){

                        $spec_data = [];

                        $spec_data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                        if($lease_set == GoodsService::GOODS_RENT_TYPE_MONTH){
                            $spec_data['lease_time'] = $v['lease_time'];
                            $spec_data['lease_price'] = $v['lease_price'];
                            $spec_data['day_price'] = bcdiv($v['lease_price'],30,2);
                        }else{
                            $spec_data['lease_time'] = $v['lease_time'];
                            $spec_data['lease_price'] =  sprintf('%.2f',bcdiv($v['day_price']*$v['lease_time'],$v['lease_time']/30,2));
                            $spec_data['day_price'] = $v['day_price'];
                        }
                        $spec_data['encoding'] = $v['encoding'];

                        $this->where('id',$spec_id)->update($spec_data);

                        $pact_type = Goods::where('id',$goods_id)->value('pact_type');

                    }else{
                        $spec_data = [];
                        $spec_data['goods_id'] = $goods_id;
                        $spec_data['setmeal_id'] = $setmeal_id;
                        $spec_data['lease_time'] = $v['lease_time'];
                        $spec_data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                        $spec_data['encoding'] = $v['encoding'];
                        if($lease_set == GoodsService::GOODS_RENT_TYPE_MONTH){
                            $spec_data['lease_price'] = $v['lease_price'];
                            $spec_data['day_price'] = bcdiv($v['lease_price'],30,2);
                        }else{
                            $spec_data['lease_price'] = sprintf('%.2f',bcdiv($v['day_price']*$v['lease_time'],$v['lease_time']/30,2));
                            $spec_data['day_price'] = $v['day_price'];
                        }
                        $spec_id = $this->insertGetId($spec_data);
                    }
                }
            }

        }
    }

}
